<template>
    <div class="pd-30">
        <div class="flex">
            <div class=" center">
                <p>用户名：</p>
                <el-input class="w-280" v-model="account" placeholder="请输入"></el-input>
            </div>
            <div class=" center">
                <p>姓名：</p>
                <el-input class="w-280" v-model="name" placeholder="请输入"></el-input>
            </div>
            <div class=" center">
                <p>手机号：</p>
                <el-input class="w-280" v-model="phone" placeholder="请输入手机号"></el-input>
            </div>
            <div class=" center">
                <p>状态：</p>
                <el-select class="w-280" v-model="value" placeholder="请选择">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </div>
        </div>
        <div class="flex mt-10">
            <div class="flex">
                <div class=" center">
                    <p class="mr-15">角色：</p>
                    <el-select class="w-280" v-model="value1" placeholder="请选择">
                        <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </div>
                <el-input v-if="value1 != 1 && value1 != ''" class="w-280 ml-194" v-model="input"
                    :placeholder="`请输入${options1[value1 - 1].label}名称`"></el-input>
            </div>
            <div class="flex">
                <el-button class="w-100">重置</el-button>
                <div class="w-30"></div>
                <el-button class="w-100" type="primary" plain @click="getUserList">搜索</el-button>
                <div class="w-30"></div>
                <el-button class="w-100" type="primary" @click="addUser">添加</el-button>
            </div>
        </div>
        <div class="h-30"></div>
        <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="id" label="编号">
            </el-table-column>
            <el-table-column prop="account" label="账号">
            </el-table-column>
            <el-table-column prop="name" label="姓名">
            </el-table-column>
            <el-table-column prop="phone" label="手机号">
            </el-table-column>
            <el-table-column prop="mail" label="邮箱">
            </el-table-column>
            <el-table-column prop="status" label="状态">
                <template #default="{ row }">
                    <p v-if="row.status == 1">正常</p>
                    <p class="red" v-else>禁用</p>
                </template>
            </el-table-column>
            <el-table-column prop="platform" label="系统">
                <template #default="{ row }">
                    <p v-if="row.platform == 1">总部系统</p>
                    <p v-else-if="row.platform == 2">服务商系统</p>
                    <p v-else-if="row.platform == 3">合伙人系统</p>
                    <p v-else-if="row.platform == 4">交付中心系统</p>
                </template>
            </el-table-column>
            <el-table-column prop="admin" :label="options1[value1 - 1].label" v-if="value1 != 1 && value1 != ''">
                <template #default="{ row }">
                    <div>{{ row.thirdName }}</div>
                </template>
            </el-table-column>
            <el-table-column prop="submitTime" label="创建时间">
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <!-- <el-button size="mini" type="success">设置上级</el-button> -->
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>

                </template>
            </el-table-column>
        </el-table>
        <div class="flex mt-20">
            <div></div>
            <el-pagination :current-page="page" background layout="prev, pager, next" :total="total" @current-change="sizeChange">
            </el-pagination>
        </div>


        <el-dialog :visible.sync="show" width="30%" :show-close="false">
            <span slot="title" class="dialog-footer">
                <h4 class="text-center">新增用户</h4>
            </span>
            <div class="plr-30  scroll">
                <div class="flex_l">
                    <div class="w-80">账号：</div>
                    <el-input class="w-350 ml-20" v-model="form.account" placeholder="请输入"></el-input>
                </div>
                <div class="flex_l mt-20">
                    <div class="w-80">头像：</div>
                    <el-upload class="ml-20" :action="upUrl" list-type="picture-card" :on-success="handlePictureCardPreview"
                        :on-remove="handleRemove" :limit="1" :file-list="imgList">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                </div>
                <div class="flex_l mt-20">
                    <div class="w-80">邮箱：</div>
                    <el-input class="w-350 ml-20" v-model="form.mail" placeholder="请输入"></el-input>
                </div>
                <div class="flex_l mt-20">
                    <div class="w-80">姓名：</div>
                    <el-input class="w-350 ml-20" v-model="form.name" placeholder="请输入"></el-input>
                </div>
                <div class="flex_l mt-20">
                    <div class="w-80">手机号：</div>
                    <el-input class="w-350 ml-20" v-model="form.phone" placeholder="请输入"></el-input>
                </div>
                <div class="flex_l mt-20">
                    <div class="w-80">密码：</div>
                    <el-input class="w-350 ml-20" v-model="form.pwd" placeholder="请输入"></el-input>
                </div>
                <div class="flex_l mt-20">
                    <div class="w-80">角色：</div>
                    <el-select class="w-350 ml-20" v-model="form.platform" placeholder="请选择" @change="changeJs">
                        <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </div>
                <div class="flex_l mt-20">
                    <div class="w-80">选择用户：</div>
                    <el-select class="w-350 ml-20" v-model="form.thirdId" placeholder="请选择" filterable @change="changeJs1">
                        <el-option v-for="item in userList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </div>
                <div class="flex_l mt-20">
                    <div class="w-80">备注：</div>
                    <el-input class="w-350 ml-20" size="small" v-model="form.beiZhu" type="textarea"
                        placeholder="请输入"></el-input>
                </div>
            </div>



            <span slot="footer" class="dialog-footer">
                <el-button @click="show = false">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getUserList, editUser, addUser, removeUser } from '@/api/user.js'
import { getUserList as fetchUserList } from '@/api/fws.js'
import { getUserList as hhrUserList, getcusJiaofuzhongxin } from '@/api/hhr.js'
export default {
    name: "User",
    data() {
        return {
            show: false,
            options: [{
                value: '1',
                label: '全部'
            }, {
                value: '2',
                label: '正常'
            }, {
                value: '3',
                label: '停用'
            }],
            value: '',
            options1: [{
                value: '',
                label: '全部'
            },{
                value: '1',
                label: '总部系统'
            }, {
                value: '2',
                label: '服务商系统'
            }, {
                value: '3',
                label: '合伙人系统'
            }, {
                value: '4',
                label: '交付中心系统'
            }],
            value1: '',
            input: '',
            tableData: [],
            page: 1,
            total: 0,
            form: {
                account: '',
                beiZhu: '',
                headerPic: '',
                mail: '',
                name: '',
                phone: '',
                platform: '1',
                pwd: '',
                roles: '1',
                thirdId: '',
                thirdName: ''
            },

            dialogImageUrl: '',
            type: '',//1新增 2修改
            upUrl: '',
            userList: [],
            imgList:[],
            account:'',
            name:'',
            phone:''
        }
    },
    mounted() {
        this.getUserList()
        this.upUrl = this.$upLoadUrl
    },
    methods: {
        sizeChange(e) {
            this.page = e
            this.getUserList()
        },
        async changeJs(e) {
            this.form.thirdId = ''
            this.form.thirdName = ''
            this.form.platform = e
            // this.form.platform = this.options1.filter(item => item.value === e)[0].label;
            // console.log(e)
            if (e == 2) {
                var data = await fetchUserList({ pageNum:1,pageSize:1000000 })
                this.userList = data.rows
            } else if (e == 3) {
                var data = await hhrUserList({ pageNum:1,pageSize:1000000 })
                this.userList = data.rows
            } else if (e == 4) {
                var data = await getcusJiaofuzhongxin({ pageNum:1,pageSize:1000000 })
                this.userList = data.rows
            }
        },
        changeJs1(e) {
            console.log(e)
            this.form.thirdName = this.userList.filter(item => item.id === e)[0].name;
        },
        addUser() {
            for (var i in this.form) {
                if (i != 'platform' && i != 'roles') {
                    this.form[i] = ''
                }
            }
            this.show = true
            this.type = 1
        },
        submit() {
            console.log(this.form)
            if (this.form.platform != 1 && this.form.thirdName == '') {
                this.$message({
                    message: '请选择用户',
                    type: 'error'
                });
                return
            }
            var apiFunction = this.type == 1 ? addUser : editUser
            apiFunction(this.form).then(res => {
                if (res.code == 0) {
                    this.show = false
                    this.getUserList()
                    this.$message({
                        message: this.type == 1 ? '添加成功' : '修改成功',
                        type: 'success'
                    });
                } else {
                    this.$message.error(res.msg || '失败');
                }
            })
        },
        getUserList() {
            getUserList({
                pageNum: this.page,
                pageSize: 10,
                platform: this.value1,
                name: this.name,
                account:this.account
            }).then(res => {
                this.tableData = res.rows
                this.total = res.total
            })
        },
        handleEdit(e, type) {
            for (var i in this.form) {
                this.form[i] = type[i]
            }
            this.form.id = type.id
            this.imgList = [{name:'1',url:type.headerPic}]
            this.show = true
            this.type = 2
        },
        handleDelete(e, type) {
            this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                removeUser({ ids: type.id }).then(res => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getUserList()
                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
            this.form.headerPic = ''
        },
        handlePictureCardPreview(file) {
            this.form.headerPic = file.list[0].url
        }
    }
}
</script>